package com.dyz.leetcode._08_StackAndQueue;

import java.util.ArrayDeque;

public class _394_decodeString {
    public String decodeString(String s) {
        ArrayDeque<Integer> numStack = new ArrayDeque<>();
        ArrayDeque<String> strStack = new ArrayDeque<>();
        StringBuffer sb = new StringBuffer();
        int num = 0;
        for(char c: s.toCharArray()){
            if(c>='0' && c<='9'){
                num = num*10+(c-'0');
            }else if(c=='['){
                //
                numStack.push(num);
                strStack.push(sb.toString());
                num = 0;
                sb.delete(0, sb.length());//    全部删除

            }else if(c==']'){
                String item = sb.toString();
                int itemCur = numStack.poll();
                for(int i=1; i<itemCur;i++){
                    sb.append(item);
                }
                sb.insert(0, strStack.pop());

            }else{
                sb.append(c);
            }

        }
        return sb.toString();
    }

    public static void main(String[] args) {
        new _394_decodeString().decodeString("3[a]2[bc]");
    }
}
